package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.SectionDao;
import com.gdit.zax.entitys.BaseEntity;
import com.gdit.zax.entitys.Dynamic;
import com.gdit.zax.entitys.Section;
import com.gdit.zax.utils.DBUtil;


public class SectionDaoImpl implements SectionDao {
	private PreparedStatement ps;
	private ResultSet rs;
	
	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	public SectionDaoImpl(Connection conn,HttpServletRequest request, HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}
	
	public BaseEntity addSectionTypeWhetherExist(String name) {
		String sql = "select * from t_sectiontype where Name=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			
			if(rs.next()){
				BaseEntity be = DBUtil.getobject(rs, BaseEntity.class);
				return be;
			} 
			
			sql = "insert t_sectiontype set Name=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			int resu = ps.executeUpdate();
			if (resu!=0) {
				return addSectionTypeWhetherExist(name);
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public ArrayList<BaseEntity> getAllCoursetype() {
		
		return null;
	}

	public ArrayList<Section> getAllSection() {
		
		return null;
	}

	public ArrayList<Section> getAllSection(int uid) {
		
		return null;
	}

	public Section getSectionByName(String name) {
		
		return null;
	}

	public Section getSectionInfo(int sid) {
		String sql = "select * from t_section where Id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, sid);
			rs = ps.executeQuery();
			
			if(rs.next()){
				Section be = DBUtil.getobject(rs, Section.class);
				return be;
			}	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
